﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Runtime.InteropServices;
using System.Threading;
using System.Xml.Serialization;
using System.Windows;
using System.Windows.Input;
using System.Text.RegularExpressions;

namespace iTFF.WPF.Model.GlobalHotKeys
{
    public class GlobalHotKey
    {
        #region Fields

        int _hotKeyID;

        #endregion

        #region Properties

        /// <summary>
        /// The Modifiers
        /// </summary>
        public ModifierKeys Modifiers { get; set; }

        /// <summary>
        /// The keys pressed combination, from System.Windows.Input
        /// </summary>
        public Key Key { get; set; }

        /// <summary>
        /// The virtual key value correspondance 
        /// </summary>
        public int VirtualKeys
        {
            get
            {
                return KeyInterop.VirtualKeyFromKey(Key);
            }
        }

        /// <summary>
        /// The Shortcut name
        /// </summary>
        public string Name { get; set; }

        /// <summary>
        /// The Display text
        /// </summary>
        [XmlIgnore]
        public string DisplayText { get; set; }

        /// <summary>
        /// The Hotkey ID registered to the OS
        /// </summary>
        [XmlIgnore]
        public int HotKeyID
        {
            get { return _hotKeyID; }
            set
            {
                if (_hotKeyID != value)
                {
                    _hotKeyID = value;
                    OnIsRegisteredChanged();
                }
            }
        }

        /// <summary>
        /// Identifies if the shortcut has Separator before
        /// </summary>
        [XmlIgnore]
        public bool HasSeparatorBefore { get; set; }

        /// <summary>
        /// Gets a boolean indicating if the Hot key Key and Modifiers properties is set
        /// </summary>
        public bool IsSet
        {
            get
            {
                return Key != Key.None && Modifiers != ModifierKeys.None;
            }
        }

        /// <summary>
        /// Gets a value indicating if the HotKey is registered, based on the HotKeyID
        /// </summary>
        public bool IsRegistered
        {
            get { return HotKeyID != -1; }
        }

        #endregion

        #region Contructor

        public GlobalHotKey()
        {
            HotKeyID = -1;
        }

        public GlobalHotKey(ModifierKeys modifiers, Key keys)
        {
            Modifiers = modifiers;
            Key = keys;
            HotKeyID = -1;
        }

        #endregion

        #region Events

        public event EventHandler<IsRegisteredEventArgs> IsRegisteredChanged;

        private void OnIsRegisteredChanged()
        {
            if (IsRegisteredChanged != null)
                IsRegisteredChanged(this, new IsRegisteredEventArgs(IsRegistered));
        }

        #endregion
    }
}
